<template>
  <DetailsFormBox :title="dataInfo?.table?.title" :type="route.query.type" :form="data.form" :btns="[
    {
      title: '重置',
      icon: '',
      type: '',
      hidden: !isEdit,
      click: getForm,
    },
    {
      title: '保存',
      icon: '',
      type: 'primary',
      hidden: !isEdit,
      click: (form: any) => {
        saveBtn(form)
      },
    },
  ]" :loading="data.loading" :json="dataInfo">
    <template #default="{ form }">
      <div class="formMain">
        <ElFormItemBox label="报工单号" :prop="`produceNoTaskWorkBillNo`">
          <ElInputBox :data="form" dataName="produceNoTaskWorkBillNo" placeholder="系统自动生成" disabled />
        </ElFormItemBox>
        <ElFormItemBox label="报工类型" :prop="`type`" isRules>
          <ElRadioBox :data="form" dataName="type" :options="options.type" :disabled="!isEdit" @changeBtn="(value: any) => {
            form.useEquipment = ''
          }" />
        </ElFormItemBox>
        <ElFormItemBox label="保养设备" :prop="`useEquipment`" isRules v-if="form.type === 1">
          <ElSelectBox :data="form" dataName="useEquipment" :options="options.device" :disabled="!isEdit" />
        </ElFormItemBox>
        <ElFormItemBox label="开始时间" :prop="`workBeginTime`" isRules>
          <ElDayTimeBox :data="form" dataName="workBeginTime" :disabled="!isEdit" />
        </ElFormItemBox>
        <ElFormItemBox label="结束时间" :prop="`workFinishTime`" isRules>
          <ElDayTimeBox :data="form" dataName="workFinishTime" :disabled="!isEdit" />
        </ElFormItemBox>
        <ElFormItemBox label="报工耗时" :prop="``">
          <ElInputBox :data="proxy.$getMinutesDifference(form.workBeginTime, form.workFinishTime)" unit="分钟" disabled />
        </ElFormItemBox>
        <ElFormItemBox label="报工原因" :prop="`workTaskRemark`" :isRules="form.type === 10">
          <ElInputBox :data="form" dataName="workTaskRemark" type="textarea" :disabled="!isEdit" />
        </ElFormItemBox>
      </div>
    </template>
  </DetailsFormBox>
</template>
<script setup lang="ts">
import { useListComponent } from '../pageTs'; // 引入逻辑
const { dataInfo, options } = useListComponent();
import { reactive, onMounted, getCurrentInstance } from 'vue';
const { proxy } = getCurrentInstance() as any;
const data = reactive({
  loading: false,
  loadingMsg: '',
  form: {
    produceNoTaskWorkBillCode: "", // 唯一编码
    type: "", // 类型，使用枚举 ProduceNoTaskWorkBillTypeEnum 的某个值
    useEquipment: "", // 机台设备编码 (MachineEquipment.EquipmentCode)
    workBeginTime: "", // 作业任务开始时间
    workFinishTime: "", // 作业任务结束时间
    workTaskRemark: "" // 报工原因
  },
});
import { useRoute } from 'vue-router';
// 获取当前路由对象
const route = useRoute();
import { sendMsg } from '@/utils/crossTagMag.ts';

const saveBtn = (form: any) => {
  if (form.produceNoTaskWorkBillNo) {
    //修改
    proxy.$api.ProductionBusniess.UpdateProduceNoTaskWorkBill(form)
      .then((res: any) => {
        _sun()
      })
      .catch(() => { });
  } else {
    //新增
    proxy.$api.ProductionBusniess.AddProduceNoTaskWorkBill(form)
      .then((res: any) => {
        _sun()
      })
  }
  function _sun() {
    proxy.$message('保存成功')
    sendMsg('save', true);
    // getForm()
    window.close();
  }
};
const isEdit = ref(true)
const getForm = () => {
  let _query: any = route.query
  if (!_query.produceNoTaskWorkBillNo) {
    data.loading = false
    return
  }
  proxy.$api.ProductionBusniess.GetMatchPageProduceNoTaskWorkBill({
    pageIndex: 1,
    pageSize: 1,
    produceNoTaskWorkBillNo: _query.produceNoTaskWorkBillNo,
  })
    .then((res: any) => {
      proxy.$message('获取成功')
      isEdit.value = (['1', '2'].includes(_query.type))
      data.form = res.list[0] || {}
      data.loading = false
    })
    .catch(() => {
      data.loading = false
    });
};
import { getDevice } from '@/utils/optionsArr.ts';
onMounted(() => {
  getForm()
  getDevice(options, 'device') //获取生产设备
});
</script>
